package com.crack.cracktool;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Map;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;

/**
 * 代码只是为了演示入门方法，在Android 4.4.4系统通过测试。
 * 实际存在一些已知问题，比如，因为权限控制第37行代码在高版本的Android系统会执行失败。
 */
@SuppressWarnings("SpellCheckingInspection")
@SuppressLint("UnsafeDynamicallyLoadedCode")
public class main implements IXposedHookLoadPackage {
    private static final String TAG = "Q849040099";
    private static final String DUMP_LUA_SO = Environment.getDataDirectory()
            + "/data/com.crack.cracktool/lib/libdumpLua.so";
    File logFile = new File("/xpdebug.log");



    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpp) throws Throwable {
        PrintWriter logWriter = new PrintWriter(new FileWriter(logFile, true));

        Log.e("849040099",String.format("检测到-----%s",lpp.packageName));
        if(lpp.packageName.equals("com.tencent.mm")){
            findAndHookMethod(
                    lpp.classLoader.loadClass("com.tencent.wcdb.database.SQLiteDatabase"),
                    "insert",
                    String.class,
                    String.class,
                    ContentValues.class,
                    new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            super.beforeHookedMethod(param);
                        }

                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);
                            ContentValues contentValues = (ContentValues) param.args[2];
                            String msg ="";
                            for (Map.Entry<String,Object> stringObjectEntry : contentValues.valueSet()) {
                                msg+="(\n" + stringObjectEntry.getKey()+":" +stringObjectEntry.getValue()+"\n)";
                            }
                            Log.e("msg8989",msg);
//                            sendServerMsg(msg);
                        }
                    }
            );
        }

//        //dump luacom.tencent.mm
//        if (!lpp.packageName.equals("com.tencent.mm")){
//
//
//            return;
//        }

        Log.e("849040099",String.format("开始hook-----%s",lpp.packageName));

        findAndHookMethod(Runtime.class, "doLoad", String.class, ClassLoader.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                String name = (String) param.args[0];

                Log.e("849040099",String.format("Load so file:%s",name));
                //Hook 要加载的 so
                //当 加载 的 so 是 要 加载的 就加载自己的
                if (param.hasThrowable() || name == null || !name.endsWith("libgame.so")) {
                    return;
                }
                Log.e("849040099",String.format("开始加载so  %s",DUMP_LUA_SO));
                System.load(DUMP_LUA_SO);// 因为权限控制，在高版本的Android系统会执行失败
                Log.e("849040099","加载结束");

            }
        });


//        Class clazz = XposedHelpers.findClass("com.tencent.mm.plugin.setting.ui.setting.ColorfulChatroomQRCodeUI", lpp.classLoader);
//        findAndHookMethod( lpp.classLoader.loadClass("com.tencent.mm.plugin.setting.ui.setting.ColorfulChatroomQRCodeUI"), "getUserName", new XC_MethodHook() {
//            @Override
//            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//
//                try {
//                    String result  =(String)param.getResult();
//                    param.setResult("43525396281@chatroom");
//                    Log.e("849040099qqq","result"+result);
//                } catch (Exception  e ) {
//                    Log.e("849040099qqq","55555555555555555555555555555"+e.toString(), e);
//                }
//                Log.e("849040099qqq","加载到ChatroomInfoUI");
//
//                Log.e("849040099qqq","加载结束");
//
//            }
//        });
//
//
//       try{
//           findAndHookMethod( lpp.classLoader.loadClass("r50.z"), "q", Class.class,new XC_MethodHook() {
//               @Override
//               protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//
//                   try {
//                       Object result  =param.getResult();
//                       String s = result.getClass().toString();
//
////                    param.setResult("43525396282@chatroom");
//                       Log.e("849040099qqq","r50-"+s);
//                   } catch (Exception  e ) {
//                       Log.e("849040099qqq","55555555555555555555555555555"+e.toString(), e);
//                   }
//                   Log.e("849040099qqq","加载到r50");
//
//                   Log.e("849040099qqq","r50加载结束");
//
//               }
//           });
//       }catch (Exception e) {
//           Log.e("849040099","55555555555555555555555555555"+e.toString(), e);
//       }
//
//        Log.e("849040099",String.format("开始hook222222222222222222222222222222-----%s",lpp.packageName));
//
//        XposedBridge.hookAllMethods(ClassLoader.class, "loadClass", new XC_MethodHook() {
//
//            @Override
//            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                if (param.args[0] instanceof String) {
//                    String className = (String) param.args[0];
//                    Log.d("HOOK", "ClassLoader: " + className);
//                }
//                if (param.thisObject instanceof ClassLoader) {
//                    ClassLoader classLoader = (ClassLoader) param.thisObject;
//                    String classLoaderName = classLoader.getClass().getName();
//                    Log.d("HOOK", "ClassLoaderqqqqqqqqqqqqqqqqqqqqqqqqq: " + classLoaderName);
//                }
//            }
//
//            @Override
//            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
////                super.afterHookedMethod(param);
//                String className= (String) param.args[0];
//                Log.e("849040099","类名:"+className);
//
//
//                XposedBridge.log("类名:"+className);
//                final String hookClassName="com.tencent.matrix.util.MemInfo";
//                if(!className.equals(hookClassName))return;
//
//
//
//
//                try{
//                    Class clz = (Class) param.getResult();
//                    if(clz == null){
//
//                    }
//                    Method[] methods = clz.getDeclaredMethods();
//
//
//                    for (final Method method : methods) {
//
//                        Class<?>[] parameterTypes = method.getParameterTypes();
//                        final String name =method.getName();
//                        XposedBridge.hookMethod(method, new XC_MethodHook() {
//                            @Override
//                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                                super.beforeHookedMethod(param);
//
//                                Log.e("HOOK", "Before " + name);
//                            }
//
//                            @Override
//                            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                                super.afterHookedMethod(param);
//                                Log.e("HOOK", "After " + name);
//                            }
//                        });
//                    }
//
//                }catch (Exception e){
//                    Log.e("849040099","55555555555555555555555555555"+e.toString(), e);
//                }
//
//                Log.e("849040099","tttttttttttttttttttttttttttt:"+className);
//
//            }
//        });
//        Log.e("849040099",String.format("333333333333333333333333333-----%s",lpp.packageName));
//        XposedBridge.hookAllConstructors(ClassLoader.class, new XC_MethodHook() {
//            @Override
//            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                super.afterHookedMethod(param);
//                Log.e("849040099", "hooked constructor: " + param.thisObject.getClass().getName());
//
//            }
//        });
//
//
//
//
//        Log.e("849040099",String.format("444444444444444444444444444444-----%s",lpp.packageName));
//        findAndHookMethod("com.tencent.matrix.util.MemInfo", lpp.classLoader, "MemInfo", Bundle.class, new XC_MethodHook() {
//            @Override
//            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                // do somthing
//                Log.e("849040099","MemInfo");
//
//                Log.e("849040099","加载结束");
//            }
//
//            @Override
//            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                // do somthing
//                Log.e("849040099","MemInfo");
//
//                Log.e("849040099","加载结束");
//            }
//        });





        // TODO: 2018/12/11
//        if(lpp.packageName.contains("mono")){
//            Log.e("发现mono","libmono.so");
//        }


    }



}
